<?php
//全局配置
ini_set ( 'display_errors', 1 );
error_reporting ( E_ALL );

include("ez_sql_core.php");
include("ez_sql_mysql.php");

$vcap_service=json_decode(getenv("VCAP_SERVICES"),true);
if($vcap_service['mysql-5.1'][0]['name']=='drift-bottles'){
	$dbconfig=$vcap_service['mysql-5.1'][0]['credentials'];
	define("DB_IP", $dbconfig['host']);
	define("DB_NAME", $dbconfig['name']);
	define("DB_USER", $dbconfig['user']);
	define("DB_PASSWD", $dbconfig['password']);
}else{
	echo "MySql Config fail";
}

$keytype=array('excludekey','bluekeyword','redkeyword');

function get_key($type){
	$db = new ezSQL_mysql(DB_USER,DB_PASSWD,DB_NAME,DB_IP);
	$db->query("set names utf8");
	$keys=$db->get_col("SELECT * FROM `".$type."`",'1');
	return base64_encode(implode(" ",$keys)); 
}
function add_key($type,$key){
	$db = new ezSQL_mysql(DB_USER,DB_PASSWD,DB_NAME,DB_IP);
	$db->query("set names utf8");
	$result=$db->query("INSERT INTO `".$type."(`key`) VALUES ('".$key."')");
	return $result;
}
function del_key($type,$key){
	$db = new ezSQL_mysql(DB_USER,DB_PASSWD,DB_NAME,DB_IP);
	$db->query("set names utf8");
	$result=$db->query("DELETE FROM `".$type."` WHERE `key`='".$key."'");
	return $result;
}
function get_blacklist(){
	$db = new ezSQL_mysql(DB_USER,DB_PASSWD,DB_NAME,DB_IP);
	$db->query("set names utf8");
	$result=$db->get_col("SELECT * FROM `blacklist`",'1');
	return $result;
}
function check_blacklist($otheruin){
	$db = new ezSQL_mysql(DB_USER,DB_PASSWD,DB_NAME,DB_IP);
	$db->query("set names utf8");
	$result=$db->get_row("SELECT * FROM `blacklist` WHERE `otheruin` ='".$otheruin."'");
	if(empty($result))
		return 0;
	else
		return 1;
}
function add_balcklist($otheruin){
	$db = new ezSQL_mysql(DB_USER,DB_PASSWD,DB_NAME,DB_IP);
	$db->query("set names utf8");
	$result=$db->query("INSERT INTO `blacklist` (`otheruin`) VALUES ('".$otheruin."')");
	return $result;
}
function add_like($otheruin){
	$db = new ezSQL_mysql(DB_USER,DB_PASSWD,DB_NAME,DB_IP);
	$db->query("set names utf8");
	$result=$db->query("INSERT INTO `likeuser` (`otheruin`) VALUES ('".$otheruin."')");
	return $result;
}